﻿namespace Example
{
    using NHibernate.Cfg;
    using NHibernate.Tool.hbm2ddl;
    using Xunit;

    public class NHibernateFacts
    {
        [Fact]
        public void Contact()
        {
            var contact = new Contact
            {
                Name = "John Doe"
            };

            var config = new Configuration();
            config.Configure();
            config.AddAssembly(typeof(Contact).Assembly);

            using (var factory = config.BuildSessionFactory())
            {
                using (var session = factory.OpenStatelessSession())
                {
                    using (var connection = session.Connection)
                    {
                        new SchemaExport(config).Execute(false, true, false, connection, null);
                        using (session.BeginTransaction())
                        {
                            session.Insert(contact);

                            Assert.Equal("John Doe", session.Get<Contact>(contact.Id).Name);

                            foreach (var item in session.CreateQuery("from Contact").List<Contact>())
                            {
                                Assert.Equal("John Doe", item.Name);
                            }
                        }
                    }
                }
            }
        }
    }
}